load("@rules_proto//proto:defs.bzl", "proto_library")
load("//:java_grpc_library.bzl", "java_grpc_library")
load("@rules_jvm_external//:defs.bzl", "artifact")

java_library(
    name = "s2a_channel_pool",
    srcs = glob([
        "src/main/java/io/grpc/s2a/internal/channel/*.java",
    ]),
    deps = [
        "//api",
        "//core",
        "//core:internal",
        "//netty",
        artifact("com.google.code.findbugs:jsr305"),
        artifact("com.google.errorprone:error_prone_annotations"),
        artifact("com.google.guava:guava"),
        artifact("org.checkerframework:checker-qual"),
        artifact("io.netty:netty-common"),
        artifact("io.netty:netty-transport"),
    ],
)

java_library(
    name = "s2a_identity",
    srcs = ["src/main/java/io/grpc/s2a/internal/handshaker/S2AIdentity.java"],
    deps = [
        ":common_java_proto",
        artifact("com.google.errorprone:error_prone_annotations"),
        artifact("com.google.guava:guava"),
    ],
)

java_library(
    name = "token_manager",
    srcs = glob([
        "src/main/java/io/grpc/s2a/internal/handshaker/tokenmanager/*.java",
    ]),
    deps = [
        ":s2a_identity",
        artifact("com.google.code.findbugs:jsr305"),
        artifact("com.google.guava:guava"),
    ],
)

java_library(
    name = "s2a_handshaker",
    srcs = [
        "src/main/java/io/grpc/s2a/internal/handshaker/ConnectionClosedException.java",
        "src/main/java/io/grpc/s2a/internal/handshaker/GetAuthenticationMechanisms.java",
        "src/main/java/io/grpc/s2a/internal/handshaker/ProtoUtil.java",
        "src/main/java/io/grpc/s2a/internal/handshaker/S2AConnectionException.java",
        "src/main/java/io/grpc/s2a/internal/handshaker/S2APrivateKeyMethod.java",
        "src/main/java/io/grpc/s2a/internal/handshaker/S2AProtocolNegotiatorFactory.java",
        "src/main/java/io/grpc/s2a/internal/handshaker/S2AStub.java",
        "src/main/java/io/grpc/s2a/internal/handshaker/S2ATrustManager.java",
        "src/main/java/io/grpc/s2a/internal/handshaker/SslContextFactory.java",
    ],
    deps = [
        ":token_manager",
        ":common_java_proto",
        ":s2a_channel_pool",
        ":s2a_identity",
        ":s2a_java_proto",
        ":s2a_java_grpc_proto",
        "//api",
        "//core:internal",
        "//netty",
        "//stub",
        artifact("com.google.code.findbugs:jsr305"),
        artifact("com.google.errorprone:error_prone_annotations"),
        artifact("com.google.guava:guava"),
        artifact("org.checkerframework:checker-qual"),
        "@com_google_protobuf//:protobuf_java",
        artifact("io.netty:netty-common"),
        artifact("io.netty:netty-handler"),
        artifact("io.netty:netty-transport"),
    ],
)

java_library(
    name = "s2av2_credentials",
    srcs = ["src/main/java/io/grpc/s2a/S2AChannelCredentials.java"],
    visibility = ["//visibility:public"],
    deps = [
        ":s2a_channel_pool",
        ":s2a_handshaker",
        ":s2a_identity",
        "//api",
        "//core:internal",
        "//netty",
        artifact("com.google.code.findbugs:jsr305"),
        artifact("com.google.errorprone:error_prone_annotations"),
        artifact("com.google.guava:guava"), 
        artifact("org.checkerframework:checker-qual"), 
    ],
)

# bazel only accepts proto import with absolute path.
genrule(
    name = "protobuf_imports",
    srcs = glob(["src/main/proto/grpc/gcp/s2a/*.proto"]),
    outs = [
        "protobuf_out/grpc/gcp/s2a/s2a.proto",
        "protobuf_out/grpc/gcp/s2a/s2a_context.proto",
        "protobuf_out/grpc/gcp/s2a/common.proto",
    ],
    cmd = "for fname in $(SRCS); do " +
          "sed 's,import \",import \"s2a/protobuf_out/,g' $$fname > " +
          "$(@D)/protobuf_out/grpc/gcp/s2a/$$(basename $$fname); done",
)

proto_library(
    name = "common_proto",
    srcs = [
        "protobuf_out/grpc/gcp/s2a/common.proto",
    ],
)

proto_library(
    name = "s2a_context_proto",
    srcs = [
        "protobuf_out/grpc/gcp/s2a/s2a_context.proto",
    ],
    deps = [
        ":common_proto",
    ],
)

proto_library(
    name = "s2a_proto",
    srcs = [
        "protobuf_out/grpc/gcp/s2a/s2a.proto",
    ],
    deps = [
        ":common_proto",
        ":s2a_context_proto",
    ],
)

java_proto_library(
    name = "s2a_java_proto",
    deps = [":s2a_proto"],
)

java_proto_library(
    name = "s2a_context_java_proto",
    deps = [":s2a_context_proto"],
)

java_proto_library(
    name = "common_java_proto",
    deps = [":common_proto"],
)

java_grpc_library(
    name = "s2a_java_grpc_proto",
    srcs = [":s2a_proto"],
    deps = [":s2a_java_proto"],
)
